package edu.pku.sei.transformation.editor.controller.editparts.link;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractLinkEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.TransformationtConnetionEditPolicy;

public class LinkTemplateEditpart extends AbstractLinkEditPart {
	private RotatableDecoration targetDecor;

	public LinkTemplateEditpart(MetaShapeElement conn) {
		super(conn);
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new TransformationtConnetionEditPolicy());
	}

	private PolylineDecoration createDecoration() {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		updateTargetDecoration();
	}

	protected void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}

	@Override
	protected IFigure createFigure() {
		return new PolylineConnection();
	}

	private PolylineConnection getConnection() {
		return (PolylineConnection) getFigure();
	}
}
